<?php

abstract class E_ACB {

// --------------------------------------------magic methods begin------------------------------------------------------------------------------

	function E_ACB() {
		$this->application_before_filter();
	}

// --------------------------------------------magic methods end--------------------------------------------------------------------------------


	function application_before_filter() {
		if (isset($this->before_filter) && is_array($this->before_filter)) {
			foreach ($this->before_filter as $function_name) {
				if (method_exists($this,$function_name)) {$this->$function_name(); } 
				else {echo "before filter method does not exist"; }
			}
		}
	}


	function process_uploaded_images_for_model($files_images) {
		$images_array = array();
		while(list($key,$value) = each($files_images['error'])) {
			$image = array("name" => $files_images["name"][$key], "type" => $files_images["type"][$key], "tmp_name" => $files_images["tmp_name"][$key], "error" => $files_images["error"][$key], "size" => $files_images["size"][$key]);
			array_push($images_array, $image);
		}
		return $images_array;
		
	}


	function setnotice($notice) {
		$_SESSION['notice'] = $notice;
	}


	function render( $options = array() ) {
		$controller = isset($options['controller']) ? $options['controller'] : $_GET['controller'];
		$action = isset($options['action']) ? $options['action'] : $_GET['action'];
		if (isset($options['json']) || isset($options['xml'])) {
			header('Cache-Control: no-cache, must-revalidate');
			header('Expires: Wed, 3 Jun 2009 13:11:43 GMT');
			if (isset($options['json'])) {
				header('Content-type: application/json');
				if ($options['json'] == 'view' && (file_exists($viewPage = APPLICATION_PATH."views/{$controller}/{$action}.php"))) { $options['json'] = require $viewPage; } //just return stuff on viewpage
				die(json_encode($options['json']));
			}
			header('Content-Type: text/xml; charset=utf-8');
			die('xml stuff');
		}
		elseif (isset($options['text'])) { die($options['text']); }

		$helperspath = APPLICATION_PATH.'helpers'.DS;
		require ($helperspath.'application_helper.php');
		if (file_exists($specialhelperfile = $helperspath.$controller.'_helper.php')) {require $specialhelperfile;}
		ob_start();
		require(APPLICATION_PATH . "views/{$controller}/{$action}.php");
		$yield = ob_get_clean();
		$layoutpath = APPLICATION_PATH.'views'.DS.'layouts'.DS;
			if (file_exists($speciallayoutfile = $layoutpath.$controller. '_layout.php')) {(require($speciallayoutfile));}
			else {require($layoutpath.'application_layout.php');}
		return true;
	}

	/**
	* essentially builds/constructs page
	* @return void
	*/
	protected function layout($action) {
		$view = $action;
		if (isset($this->content) && $this->content == 'xml') {
			header('Content-Type:text/xml; charset=utf-8');
			header('Cache-Control: no-cache, must-revalidate');
			$view .= '_view';
			return $this->$view();
		}
		elseif (isset($this->content) && $this->content == 'text') {
			$view .= '_view';
			return $this->$view();
		}
		header('Content-Type:text/html;charset=utf-8');
		$this->pg_header($view);
		$view .= '_view';
		$this->$view();
		$this->pg_footer();
	}

	function renderXML( $controller,  $action ) {
		$XMLfile = APPLICATION_PATH.'views'.DS.$controller.DS.$action .'XML.php';
		if (file_exists($XMLfile)) { require($XMLfile); }
		else { echo $XMLfile.' does not exist!'; }
		exit();
	}

	function renderAJAXText( $controller,  $action ) {
		$textFile = APPLICATION_PATH.'views'.DS.$controller.DS.$action;
		if (file_exists($textFile)) { require($textFile); }
		else { echo $textFile.' does not exist!'; }
		exit(0);
	}



	function redirect($controller = null, $action, $id = null) {
		$loc = PUBLIC_HTML;
		if ($controller) { $loc .= "$controller/$action"; }
		else { $loc .= "$action"; }
		if ($id != null) {$loc .= "/$id";}
		return header("Location: $loc");
		exit(0);
	}


	function login() {
		if ($this->isUserLoggedIn()) { return $this->redirect(CONTROLLER, 'menu' ); }
		return $this->render( "staff" , 'login' );
	}


	function isUserLoggedIn() {
		return isset($_SESSION['login_indicator_uname']);
	}


	function logout() {
		if ($this->isUserLoggedIn()) { $this->logout_function(); }
		else { $this->setnotice("Seems you were not loged in."); }
		return $this->redirect( CONTROLLER, "index.html" );
		
	}


	function logout_function() {
		$_SESSION['login_indicator_uname'] = null;
		unset($_SESSION['login_indicator_uname']);
		$this->setnotice("You have successfully logged out.");
		//$this->remove_session_and_cookie();
		return $this->isUserLoggedIn();
	}


	private function remove_session_and_cookie() {
		$_SESSION = array();
		if (isset($_COOKIE[session_name()])) { setcookie(session_name(), '', time()-1, '/'); } //chnage to server[req time], and serevr[sesname]
		session_destroy();
		return true;
	}




	function crud_manager() {
		//var_dump($_GET['id']);
		if ($_SERVER['REQUEST_METHOD'] == 'GET') { //new edit
			if (isset($_GET['id'])) { return 'edit'; }
			else { return 'new'; }
		}
		elseif ($_SERVER['REQUEST_METHOD'] == 'POST') { //update delete or create
			if (isset($_GET['id'])) {
				if (isset($_POST['commit']) && $_POST['commit'] == 'Edit') { return 'update'; }
				else { return 'destroy'; }
			} 
			else { return 'create'; }
		}
	}


}

?>